<?php

declare(strict_types=1);

namespace app\api\controller\v1;


use think\facade\Request;
use app\api\service\StatusCode;
use app\api\service\UserToken;
use app\api\validate\Chapter as ValidateChapter;
use app\api\model\Chapter as ModelChapter;
use app\api\validate\ParamMustBePostiveInt;
use app\api\model\Week as ModelWeek;
use app\api\model\Section as ModelSection;

/**
 * 课程章节管理类
 */
class Chapter extends Api
{
    /**
     * 获取章节
     */
    public function index()
    {
        $week_id = (new ParamMustBePostiveInt('week_id'))->paramCheck();
        $week = ModelWeek::verifyCourse($week_id);
        if ($week === false) {
            exit(json_encode([
                'code' => 400,
                'message' => '参数错误',
            ]));
        }
        $data = ModelChapter::getChapter($week_id);
        exit(json_encode([
            'code' => 200,
            'message' => '数据获取成功',
            'data' => $data
        ]));
    }

    /**
     * 
     *修改章节状态
     */

    public function editStatus()
    {
        $id = (new ParamMustBePostiveInt())->paramCheck();
        $data = Request::get();
        validate(ValidateChapter::class)->scene('editChapterStatus')->check($data);
        $chapter = ModelChapter::verifyChapter($id);
        if ($chapter === false) {
            exit(json_encode([
                'code' => 400,
                'message' => '参数错误'
            ]));
        }
        $success = ModelChapter::editChapter($chapter, $data['status'], true);
        if ($success === false) {
            exit(json_encode([
                'code' => 404,
                'message' => '参数错误'
            ]));
        }
        if ($success != true) {
            exit(json_encode([
                'code' => 404,
                'message' => '修改失败'
            ]));
        }
        $this->return_msg(StatusCode::SUCCESS, '修改成功', $success);
    }

    /**
     * 
     * 删除章节
     */

    public function delete()
    {
        $id = (new ParamMustBePostiveInt())->paramCheck();
        $chapter = ModelChapter::verifyChapter($id);
        if ($chapter === false) {
            exit(json_encode([
                'code' => 400,
                'message' => '参数错误'
            ]));
        }
        $section = ModelSection::getSection($id);
        if (!empty($section->toArray())) {
            exit(json_encode([
                'code'    =>  404,
                'message' => '删除失败,请先删除小节'
            ]));
        };
        $success = ModelChapter::delChapter($chapter);
        if ($success != true) {
            exit(json_encode([
                'code' => 400,
                'message' => '删除失败'
            ]));
        }
        exit(json_encode([
            'code' => 200,
            'message' => '删除成功'
        ]));
    }

    /**
     * # 修改章节标题
     */
    public function editTitle()
    {
        $id = (new ParamMustBePostiveInt())->paramCheck();
        $data = Request::post();
        validate(ValidateChapter::class)->scene('editChapterTitle')->check($data);
        $success = ModelChapter::editChapter($id, $data['title']);
        $this->return_msg(StatusCode::SUCCESS, '修改成功', $success);
    }

    /**
     * 
     * 增加章节内容
     */

    public function create()
    {
        $data = Request::post();
        validate(ValidateChapter::class)->scene('createChapter')->check($data);
        $data["user_id"] = UserToken::getCurrentUid();
        $data = ModelChapter::addChapter($data);
        $this->return_msg(StatusCode::SUCCESS, '新增成功', $data);
    }

    /**
     * 获取章节小节
     */
    public function read()
    {
        $chapter_id = (new ParamMustBePostiveInt('chapter_id'))->paramCheck();
        $data = ModelChapter::getSection($chapter_id);
        exit(json_encode($data));
    }
}
